#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
import json
import pymongo
import pandas as pd
import numpy as np
from config import *
client = pymongo.MongoClient(MONGO_URI)
db = client["xueqiu"]

cookies={
"_utmz":"1.1525739806.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)",
"device_id":"742b812a7655ac0be131ff6d3fe89856",
"aliyungf_tc":"AQAAAPoypzOFuAoAdDWJd0UdAKv8Xjax",
"xq_a_token":"0d524219cf0dd2d0a4d48f15e36f37ef9ebcbee1",
"xq_a_token.sig":"P0rdE1K6FJmvC2XfH5vucrIHsnw",
"xq_r_token":"7095ce0c820e0a53c304a6ead234a6c6eca38488",
"xq_r_token.sig":"xBQzKLc4EP4eZvezKxqxXNtB7K0",
"Hm_lvt_1db88642e346389874251b5a1eded6e3":"1525739851,1525740141,1525746160,1525751236",
"Hm_lpvt_1db88642e346389874251b5a1eded6e3":"1525751236",
"__utma":"1.1051211781.1525739806.1525746160.1525751237.3",
"__utmc":"1",
"__utmt":"1",
"__utmb":"1.1.10.1525751237",
"u":"971525751236873"
}

header={
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
}

def get_data():
    queryArgs = {}
    projectionFields = {'_id': False}  # 用字典指定
    result = db["不明真相的群众"].find(queryArgs,projectionFields)
    return result

def main():
    data=pd.DataFrame(list(get_data()))
    data.to_csv("fz_follwers.csv",encoding="utf-8",index=False)



def get_fangz_follwers():
    for i in range(1, 12400):
        get_alll_follwers(i)


def get_alll_follwers(page):
    print(page)
    url = "https://xueqiu.com/friendships/followers.json?uid=1955602780&pageNo={page}".format(page=page)
    try:
        res = requests.get(url, cookies=cookies, headers=header)
        if res.status_code == 200:
            follwers = json.loads(res.text)
            all = follwers.get("followers")
            print(all)
            saveToMongo(all)

        else:
            print(res.status_code)
    except Exception as e:
        print(e)

def saveToMongo(result):
    try:
        if db["不明真相的群众"].insert(result):
            print("save result success");
    except:
        print("save mongo error")

if __name__ == '__main__':
    main()